package com.hancher.plugin;

import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hancher.plugin.hrpage3.PageRecyclerViewManager;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private PageRecyclerViewManager<String> prvm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        prvm = new PageRecyclerViewManager<String>()
                .setContext(this)
                .setRecyclerView(findViewById(R.id.recyclerview))
                .setEmptyView(com.hancher.plugin.hrpage3.R.layout.hr_page_empty_view)
                .setAdapter(new BaseQuickAdapter<String, BaseViewHolder>(android.R.layout.simple_list_item_1) {
                    @Override
                    protected void convert(@NonNull BaseViewHolder baseViewHolder, String item) {
                        baseViewHolder.setText(android.R.id.text1, item);
                    }
                })
                .setDivider(true)
                .setOnItemLongClickListener((baseQuickAdapter, view, i) -> {
                    Toast.makeText(MainActivity.this, "长按了:" + prvm.getRvList().get(i), Toast.LENGTH_SHORT);
                    return true;
                })
                .setOnItemClickListener((baseQuickAdapter, view, i) -> {
                    Toast.makeText(MainActivity.this, "点击了:" + prvm.getRvList().get(i), Toast.LENGTH_SHORT);
                })
                .setRefreshListener(new OnRefreshLoadMoreListener() {
                    @Override
                    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                        queryList(PageRecyclerViewManager.LOADMORE);
                    }

                    @Override
                    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                        queryList(PageRecyclerViewManager.FRESH);
                    }
                })
                .setRefreshLayout(findViewById(R.id.refresh_layout))
                .setRefresh(true)
                .setLoadMore(true)
                .build();
    }

    private void queryList(String type) {
        prvm.setDataChangeType(type);

        // 查询数据
        int totalCount = 33;
        List<String> list = new ArrayList<>();
        int totalPage = new BigDecimal(totalCount).divide(new BigDecimal(prvm.getSize()), 0, RoundingMode.UP).intValue();
        int sizeInCurrentPage = totalPage == prvm.getPage() ? totalCount % prvm.getSize() : prvm.getSize();
        for (int i = 0; i < sizeInCurrentPage; i++) {
            list.add("第" + prvm.getPage() + "页,第" + (i + 1) + "条");
        }

        prvm.freshPageData(list, totalCount);
    }
}